* Replication
* John Gerring, Carl Henrik Knutsen, Matthew Maguire, Svend-Erik Skaaning, Jan Teorell, and Michael Coppedge. 2021. “Democracy and Human Development: Issues of Conceptualization and Measurement.” Democratization 28(2): 308-332.

* Table C10: Tests of MPI using annual data

* Model 8

* set maxvar
clear
clear matrix
clear mata
set maxvar 20000

* set directory

set matsize 10000

* load MI data
use "2021_Democratization_replication_data_annual_MI.dta", clear
qui mi import flong, m(imp) id(year country_id) imp(e_migdppc infmor_gapminder infmor_clio_ipo)
drop edi_mult_stock_10
qui mi stset, clear
qui mi xtset country_id year
qui gen e_migdppc_ln = ln(e_migdppc)
qui gen infmor_gapminder_ln = ln(infmor_gapminder)
mi merge 1:1 country_id year using "2021_Democratization_replication_data_posteriors_for_MI.dta"

* put democracy index on 0-1 scale
foreach x of varlist mpi_med_stock_10 {
quietly mi xeq: sum `x'
quietly mi xeq: replace `x' = ((`x' - `r(min)') * (1 - 0)) / (`r(max)' - `r(min)')
}
*

* Run baseline model
qui mi stset, clear
qui mi xtset country_id year
set more off
*qui mibeta F.infmor_gapminder_ln mpi_med_stock_10 e_migdppc_ln i.year i.country_id, vce(cluster country_id)
*qui local rsquared=e(r2_mi)
qui mi estimate, post: xtreg F.infmor_gapminder_ln mpi_med_stock_10 e_migdppc_ln i.year, fe vce(cluster country_id)
qui outreg2 using Table_2_Model_8.xls, label se bdec(3) noni noobs nor nocon nonot drop(i.year) ctitle(" ") addstat("Years", e(g_max), "Countries", e(N_g), "Observations", e(N)) dta replace


***Monte Carlo Estimates Using UDS 3202 Draw Posterior Distribution***
*Run the monte carlo

forvalues i = 2(1)901 {

*Print out an iteration number
display `i'

* put democracy index on 0-1 scale
foreach x of varlist mpi_v`i'_stock_10 {
quietly mi xeq: sum `x'
quietly mi xeq: replace `x' = ((`x' - `r(min)') * (1 - 0)) / (`r(max)' - `r(min)')
}
*

*Fit the model, using the ith draw from the UDS posterior
set more off
preserve
*qui mibeta F.infmor_gapminder_ln mpi_v`i'_stock_10 e_migdppc_ln i.year i.country_id, vce(cluster country_id)
*qui local rsquared=e(r2_mi)
qui mi estimate, post: xtreg F.infmor_gapminder_ln mpi_v`i'_stock_10 e_migdppc_ln i.year, fe vce(cluster country_id)
qui outreg2 using Table_2_Model_8.dta, label se bdec(3) noni noobs nor nocon nonot drop(i.year) ctitle("`i'") addstat("Years", e(g_max_mi), "Countries", e(N_g_mi), "Observations", e(N_mi)) dta append


*Extract the coefficients and variance-covariance matrix
matrix b = e(b)
matrix V = e(V)
local blength = colsof(b)

*Preserve the dataset, take a single multivariate normal draw from the
*posterior distribution of the coefficients, and restore the dataset.
*We use the capture command to catch possible errors in drawnorm
*and drop these iterations gracefully.
*preserve 
capture quietly drawnorm b1-b`blength', double n(1) means(b) cov(V) clear
if _rc==0 {
mkmat b1-b`blength', matrix(bsample)
matrix posterior = nullmat(posterior) \ bsample
}
else {
display "Error drawing sample...iteration dropped"
}
restore

*Closes the Monte Carlo Loop
}

*Get posterior ready to work with
svmat posterior

*Calculate means and standard deviations
*tabstat posterior*, stat(mean sd)
keep posterior*
quietly outreg2 using Table_2_Model_8, eqkeep(mean sd) bdec(3) replace sum(log) excel
